local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local msgproxy = require "msgproxy"
local base = require "base"
local logicmng = require "logicmng"
local timetool = require "timetool"
local tabletool = require "tabletool"
local timer = require "timer"
local filename = "tablecmd.lua"
require "enum"
local TableCMD = {}

function TableCMD.process(session, source, event, ...)
	local f = TableCMD[event] 
	if f == nil then
		filelog.sys_error(filename.." TableCMD.process invalid event:"..event)
		return nil
	end
	f(...)	 
end
--[[
conf = {
	....
	conf_version = 1,
	gameid = ,
	room_type = ,
	name = ,
    max_player_num = 4,		---玩家数
    create_user_rid = ,
    create_user_rolename = ,
    create_user_logo = ,
    create_time = ,
   	action_timeout = ,    --玩家出牌时间
	action_timeout_count = ,
	iscontrol = ,
	max_watch_playernum = , --最大旁观人数

	retain_to_num = , --碰胡子游戏局数
	....
}
]]
function TableCMD.start(conf, roomsvr_id, id)
	if conf == nil or roomsvr_id == nil then
		filelog.sys_error(filename.."conf == nil or roomsvr_id == nil")
		base.skynet_retpack(false)
		return
	end

	if id ~= nil then
		conf.id = id
	end

	local server = msghelper:get_server()
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	server.table_data.state = ETableState.TABLE_STATE_WAIT_PLAYER_SITDOWN
	roomtablelogic.init(server.table_data, conf, roomsvr_id)
    --上报状态
    msghelper:report_table_state()

	base.skynet_retpack(true)
end

function TableCMD.reload(conf)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if conf.version <= table_data.conf.version then
		return
	end
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")

	---判断桌子是否在游戏中,如果是在游戏中，这将配置文件缓存
	if not roomtablelogic.is_gameend(table_data) then
		table_data.backofconf = tabletool.deepcopy(conf)
		return
	end
	for key, value in pairs(conf) do
		table_data.conf[key] = value 
	end

	if table_data.conf.gamecfg.max_player_num ~= nil then
		table_data.conf.max_player_num = table_data.conf.gamecfg.max_player_num
	end

	if table_data.conf.gamecfg.base_score ~= nil then
		table_data.conf.base_score = table_data.conf.gamecfg.base_score
	end

	if table_data.conf.gamecfg.min_carry_coin ~= nil then
		table_data.conf.min_carry_coin = table_data.conf.gamecfg.min_carry_coin
	end	
end

function TableCMD.delete(...)
	--上报桌子管理器房间被删除
	---local params = table.unpack(...)
	local server = msghelper:get_server()
	local table_data = server.table_data
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	table_data.isdeleteing = true
	if roomtablelogic.get_sitdown_player_num(table_data) > 0 then
		--踢出座位上的玩家
		roomtablelogic.leavetableallplayer(table_data, EStandupReason.STANDUP_REASON_DELETE_TABLE)

		local waits = tabletool.deepcopy(table_data.waits)
		for rid, waitinfo in pairs(waits) do
			roomtablelogic.passive_leavetable(table_data, rid, true)
		end
	end	


	msgproxy.sendrpc_broadcastmsgto_tablesvrd("delete", table_data.conf.gameid, table_data.svr_id , table_data.id)

	--通知roomsvrd删除table
	skynet.send(table_data.svr_id, "lua", "cmd", "delete_table", table_data.id)
		
	--删除桌子前清除桌子的状态
	roomtablelogic.clear(table_data)
	--延迟释放桌子
	skynet.sleep(10)
	
	server:exit_service()
end

return TableCMD